# ----------------------
# 参数解析
# ----------------------
param (
    [string]$build_type,
    [string]$install_dir
)

# 设置错误立即退出
$ErrorActionPreference = "Stop"

# 保存当前目录
$curr = Get-Location

# 默认参数
$BUILD_TYPE = "Release"
$INSTALL_DIR = Join-Path $PWD "install"



if ($build_type) { $BUILD_TYPE = $build_type }
if ($install_dir) { $INSTALL_DIR = $install_dir }

# ----------------------
# 项目根目录（相对于脚本位置）
# ----------------------
$project_dir = Join-Path $PSScriptRoot ".."

Set-Location $project_dir

# ----------------------
# CMake 配置
# ----------------------
cmake -B build -S . -DCMAKE_BUILD_TYPE=$BUILD_TYPE

# 并行编译（PowerShell 里用环境变量 NUMBER_OF_PROCESSORS）
$parallel = $env:NUMBER_OF_PROCESSORS
cmake --build build --config $BUILD_TYPE --parallel $parallel

# 安装
cmake --install build --prefix $INSTALL_DIR

# 返回原目录
Set-Location $curr

Write-Host "Build ($BUILD_TYPE) and install completed successfully in $INSTALL_DIR!"
